package com.stack.knowyouweb.controller;

import com.google.gson.Gson;

import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoubean.bean.RepJson;
import com.stack.knowyouservice.service.DataAnalysisService;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 数据分析相关接口响应层
 *
 * @author stack
 */
@RestController
@Scope("singleton")
@RequestMapping("/api")
public class DataAnalysisController {

    @Resource
    private DataAnalysisService dataAnalysisService;
    @Resource
    private Gson gson;

    @GetMapping("/examanalysis")
    public String examAnalysis(Integer hid, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            Map<String, Object> map = dataAnalysisService.examAnalysis(hid, sid);
            RepJson<Map<String, Object>> repJson = new RepJson<>();
            repJson.setData(map);
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("分析成功");
            return gson.toJson(repJson);
        }
    }

    @GetMapping("/prehomworkanalysis")
    public String preHomeWorkAnalysis(Integer hid, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            Map<String, Object> map = dataAnalysisService.preHomeWorkAnalysis(hid, sid);
            RepJson<Map<String, Object>> repJson = new RepJson<>();
            repJson.setData(map);
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("分析成功");
            return gson.toJson(repJson);
        }
    }

    @GetMapping("/stu/score/analysis")
    public String stuScoreAnalysis(Integer hkid, HttpSession session, Authentication authentication) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            Map<String, Object> map = dataAnalysisService.stuScoreAnalysis(hkid, sid, authentication);
            RepJson<Map<String, Object>> repJson = new RepJson<>();
            repJson.setData(map);
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("分析成功");
            return gson.toJson(repJson);
        }
    }


    @GetMapping("/teacher/score/analysis")
    public String teacherScoreAnalysis(String param, HttpSession session, Authentication authentication) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            Map<String, Object> map = dataAnalysisService.teacherScoreAnalysis(param, sid);
            RepJson<Map<String, Object>> repJson = new RepJson<>();
            repJson.setData(map);
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("分析成功");
            return gson.toJson(repJson);
        }
    }


}
